package com.itbuka.order.listener;

import com.itbuka.order.domain.Task;
import com.itbuka.order.service.TaskService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class Scheduling {
    @Autowired
    private TaskService taskService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Scheduled(cron = "0 0/2 * * * ?")
    public void time() {
        //3、写个定时任务把task表中的数据发送给MQ
        List<Task> tasks = taskService.selectAll();
        if (tasks != null && tasks.size() > 0) {
            for (Task task : tasks) {
                rabbitTemplate.convertAndSend(task.getMqExchange(), task.getMqRoutingkey(), task.getRequestBody());
            }
        }
    }
}
